package com.e3.basdoc.dept.client;

import java.util.List;

import com.e3.basdoc.dept.model.DeptModel;
import com.e3.web.widget.client.MyDialog;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class DeptDetailGridDialog extends MyDialog {

	private Grid<DeptModel> deptGridPanel;

	private GridSelectionModel<DeptModel> gridSelectionModel = new GridSelectionModel<DeptModel>();

	private ListStore<DeptModel> deptGridStore = new ListStore<DeptModel>();

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		super.setLayout(new FitLayout());
		this.setSize(600, 450);
		this.setHeading(DeptMessages.INSTANCE.title_dept_detaildialog());
		add(getDeptGrid());
	}

	private Grid<DeptModel> getDeptGrid() {
		if (deptGridPanel == null) {
			List<ColumnConfig> configs = DeptHelper.INSTANCE.getDeptGridColumnConfigs();
			ColumnModel cm = new ColumnModel(configs);
			deptGridStore.setModelComparer(DeptHelper.INSTANCE.getModelComparer());
			deptGridStore.setKeyProvider(DeptHelper.INSTANCE.getModelKeyProvider());
			deptGridPanel = new Grid<DeptModel>(deptGridStore, cm);
			deptGridPanel.setSelectionModel(gridSelectionModel);
		}
		return deptGridPanel;
	}

	public void setDepts(List<DeptModel> deptModels) {
		deptGridStore.removeAll();
		deptGridStore.add(deptModels);
	}

	public void addDepts(List<DeptModel> deptModels) {
		deptGridStore.add(deptModels);
	}

	public List<DeptModel> getDepts() {
		return deptGridStore.getModels();
	}

	public void showDialog(List<DeptModel> depts) {
		deptGridStore.removeAll();
		if (depts != null) {
			deptGridStore.add(depts);
		}
		setVisible(true);
	}

	@Override
	protected void onOk() {
		setVisible(false);
	}

}
